package com.metagurukul.metaspace.actions;

import java.sql.Date;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.metagurukul.metaspace.controller.PersonalDetailsForm;
import com.metagurukul.metaspace.models.beans.UserPersonalDetail;
import com.metagurukul.metaspace.service.UserService;

/**
 * Represents an action class which takes personal details from user then
 * validates it that either they are completed or not and sends error report to
 * user.
 * 
 * @author bhagyesh
 * 
 */
public class PersonalDetailsAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException {
		PersonalDetailsForm personalDetailsForm = (PersonalDetailsForm) form;
		String userName = personalDetailsForm.getUserName();
		String name = personalDetailsForm.getName();
		String contact = personalDetailsForm.getContact();
		char gender = personalDetailsForm.getGender().charAt(0);
		String date[] = null;
		int day = -1;
		int month = -1;
		int year = -1;
		try {
			date = personalDetailsForm.getDate().split("-");
			day = Integer.parseInt(date[0]);
			month = Integer.parseInt(date[1]);
			year = Integer.parseInt(date[2]);
		} catch (Exception e) {
			ActionMessages errors = new ActionMessages();
			errors.add("error", new ActionMessage("error.dob.invalid"));
			return mapping.getInputForward();
		}
		String profilepicpath;
		if (gender == 'M') {
			profilepicpath = "0male.jpg";
		} else {
			profilepicpath = "female.jpg";
		}
		Date dob = new Date(year - 1900, month - 1, day);
		HttpSession session = request.getSession();
		int userId;
		try {
			userId = (Integer) session.getAttribute("id");
		} catch (NullPointerException e) {
			ActionMessages errors = new ActionMessages();
			errors.add("error", new ActionMessage("error.userId.invalid"));
			return mapping.getInputForward();
		}
		int flag = insertPersonalDetail(userId, name, dob, contact, gender,
				userName, profilepicpath);
		if (flag > 0) {
			return mapping.findForward("success");
		} else if (flag == 0) {
			ActionMessages errors = new ActionMessages();
			errors.add("error", new ActionMessage(
					"error.personalDetails.notInsert"));
			return mapping.getInputForward();
		} else {
			ActionMessages errors = new ActionMessages();
			errors.add("error", new ActionMessage("error.userId.invalid"));
			return mapping.getInputForward();
		}
	}

	/**
	 * Sends data to service layer where it connects to and insert or update
	 * data to the record
	 * 
	 * @param userId
	 * @param name
	 * @param dob
	 * @param contact
	 * @param gender
	 * @param userName
	 * @param profilePicPath
	 * @return
	 * @throws SQLException
	 */
	public int insertPersonalDetail(int userId, String name, Date dob,
			String contact, char gender, String userName, String profilePicPath)
			throws SQLException {
		UserPersonalDetail userPersonalDetail = new UserPersonalDetail();
		userPersonalDetail.setContact(contact);
		userPersonalDetail.setDate(dob);
		userPersonalDetail.setGender(gender);
		userPersonalDetail.setName(name);
		userPersonalDetail.setProfilePicPath(profilePicPath);
		userPersonalDetail.setUserId(userId);
		userPersonalDetail.setUserName(userName);
		return UserService.insertPersonalDetail(userPersonalDetail);
	}
}